import 'dart:convert';

import 'package:flutter_demo/constants/constants.dart';
import 'package:flutter_demo/ui/graphQL_test/api/api_client.dart';
import 'package:flutter_demo/ui/graphQL_test/model/change_password_resp.dart';
import 'package:flutter_demo/ui/graphQL_test/model/country.dart';
import 'package:flutter_demo/ui/graphQL_test/model/http_response.dart';
import 'package:flutter_demo/ui/graphQL_test/model/login_req.dart';
import 'package:flutter_demo/ui/graphQL_test/model/login_response.dart';
import 'package:flutter_demo/utils/sp_util.dart';
import 'package:flutter_demo/ui/graphQL_test/model/refresh_token_req.dart'
    as refresh_token_req;
import 'package:flutter_demo/ui/graphQL_test/model/change_password.dart'
    as change_password_req;

///@date:  2024/1/18
///@author:  ZhaoPing
///@description: 登录请求

class LoginGraphQLRequest<T> extends GraphQLAPIClient {
  ///登录请求
  ///@param username 用户名
  ///@param password 密码
  Future<HttpBaseResponse<LoginResponse>> login(
      String username, String password) async {
    String query = '''
                      query ApplyModule(\$input: LoginInput!) {
                        login(input: \$input) {
                          refresh_token
                          access_token
                          expires_in
                        }
                      }
                      ''';
    var params =
        LoginReq(input: Input(username: username, password: password)).toJson();
    var result = await this.query<LoginResponse>(
        query, params, (data) => LoginResponse.fromJson(data));
    saveToken(result);
    return Future.value(result);
  }

  ///保存token
  void saveToken(HttpBaseResponse<LoginResponse> result) {
    if (result.isSuccess) {
      var token = result.data?.login?.accessToken;
      var refreshToken = result.data?.login?.refreshToken;
      Constants.token = token;
      Constants.refreshToken = refreshToken;
      if (token != null && refreshToken != null) {
        SpUtil.setString(Constants.spTokenKey, token);
        SpUtil.setString(Constants.spRefreshTokenKey, refreshToken);
      }
    }
  }

  ///登录请求
  Future<HttpBaseResponse<LoginResponse>> refreshToken() async {
    String query = '''
                      query ApplyModule(\$input: RefreshTokenInput!) {
                        refreshToken(input: \$input) {
                          refresh_token
                          access_token
                          expires_in
                        }
                      }
                      ''';

    var token = SpUtil.getString(Constants.spRefreshTokenKey) ?? "";
    var params = refresh_token_req.RefreshTokenReq(
            input: refresh_token_req.Input(refreshToken: token))
        .toJson();
    var result = await this.query<LoginResponse>(
        query, params, (data) => LoginResponse.fromJson(data));

    saveToken(result);

    return Future.value(result);
  }

  ///登录请求
  Future<HttpBaseResponse<ChangePasswordResp>> changePassword(
      oldPassword, newPassword) async {
    String query = '''
                     mutation Mutation(\$input: ChangePasswordInput!) {
                       changePassword(input: \$input)
                     }
                    ''';
    var req = change_password_req.ChangePassword(
        input: change_password_req.Input(
            oldPassword: oldPassword, newPassword: newPassword));

    var result = await this.query<ChangePasswordResp>(
        query, req.toJson(), (data) => ChangePasswordResp.fromJson(data));
    return Future.value(result);
  }


}
